package com.solutionsfit.timesheet.action

import static org.jboss.seam.annotations.FlushModeType.MANUAL
import static org.jboss.seam.ScopeType.CONVERSATION
import static org.jboss.seam.ScopeType.PAGE

import javax.persistence.NoResultExceptionimport com.solutionsfit.timesheet.GroovyTimesheet
import javax.persistence.Queryimport com.solutionsfit.timesheet.Projectimport org.jboss.seam.faces.FacesMessagesimport org.jboss.seam.faces.FacesMessagesimport javax.persistence.Query
import com.solutionsfit.timesheet.GroovyTimesheet
import com.solutionsfit.timesheet.TimesheetStatusimport com.solutionsfit.timesheet.PayPeriodType

import org.jboss.seam.annotations.Begin
import org.jboss.seam.annotations.End
import org.jboss.seam.annotations.Name
import org.jboss.seam.annotations.In
import org.jboss.seam.annotations.Out
import org.jboss.seam.annotations.Factory
import org.jboss.seam.annotations.Logger
import org.jboss.seam.annotations.Scope
import org.jboss.seam.annotations.datamodel.DataModel
import org.jboss.seam.annotations.datamodel.DataModelSelection

import org.jboss.seam.log.Log

import javax.persistence.EntityManager

@Name("timesheetEntry")
@Scope(CONVERSATION)
class TimesheetEntryAction {
	@Logger
	Log log
	
	@In
	FacesMessages facesMessages
	
	@In
	EntityManager em
	
	@DataModel(value="timesheets", scope=PAGE)
	List<GroovyTimesheet> timesheets
	
	@DataModelSelection(value="timesheets")
	GroovyTimesheet timesheetSelection
	
	@In(required=false)
	@Out(required=false)
	GroovyTimesheet newTimesheet
	
	Long projectId
	
	@Factory("timesheets")
	void loadTimesheets() {
		Calendar today = Calendar.instance

		int thisMonth = today.get(Calendar.MONTH)
		int thisYear = today.get(Calendar.YEAR)
		
		timesheets = new ArrayList<GroovyTimesheet>()
		
		PayPeriodType.values().each {
			GroovyTimesheet timesheet = null;
			
			try {
				timesheet = em.createQuery("select timesheet from GroovyTimesheet as timesheet where naturalId = ?1")
					.setParameter(1, thisMonth + "-" + thisYear + "-" + it)
					.singleResult
			} catch(NoResultException e) {
				timesheet = new GroovyTimesheet(thisMonth, thisYear, it)
			}
			
			timesheets << timesheet
		}
	}

	@Factory("projectSelections")
	List<Project> loadProjects() {
		em.createQuery("select project from Project as project").resultList
	}
	
	@Begin(flushMode=MANUAL)
	String newTimesheet() {
		newTimesheet = timesheetSelection
		
		if(newTimesheet.project != null)
			projectId = newTimesheet.project.id
			
		log.info("Filling out a new GroovyTimesheet: #0", newTimesheet)
		
		"/timesheetEntry.xhtml"
	}
	
	String persistTimesheet(TimesheetStatus status) {
		newTimesheet.status = status
		newTimesheet.project = em.find(Project.class, projectId)
		
		em.merge(newTimesheet)
		em.flush()
		
		loadTimesheets()
	}
	
	@End
	String save() {
		persistTimesheet(TimesheetStatus.UNSUBMITTED)
		
		facesMessages.add("Saved timesheet");
		
		"/timesheets.xhtml"
	}
	
	@End
	String submit() {
		persistTimesheet(TimesheetStatus.AWAITING_APPROVAL)
		
		facesMessages.add("Submitted timesheet");
		
		"/timesheets.xhtml"
	}
	
	@End
	String cancel() {
		"/timesheets.xhtml"
	}
}
